package com.hdphone.zljutils.impl;

import com.hdphone.zljutils.ZljUtils;
import com.hdphone.zljutils.inter.IMD5Util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;

/**
 * @date: 2021/6/2
 * @email： yzandlj@163.com
 * @author： yangzhi
 * @describe:
 */
public class MD5UtilImpl implements IMD5Util {

    private final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

    @Override
    public String encode(String text) {
        try {
            MessageDigest digest = MessageDigest.getInstance("MD5");
            byte[] result = digest.digest(text.getBytes());
            StringBuilder sb = new StringBuilder();
            for (byte b : result) {
                int number = b & 0xff;
                String hex = Integer.toHexString(number);
                if (hex.length() == 1) {
                    sb.append("0").append(hex);
                } else {
                    sb.append(hex);
                }
            }
            return sb.toString().toUpperCase();
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 对文件进行加密
     */
    @Override
    public String getMD5File(InputStream in) {
        if (in == null) return null;
        int numRead = 0;
        byte[] buffer = new byte[1024];
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
            while ((numRead = in.read(buffer)) > 0) {
                md5.update(buffer, 0, numRead);
            }
            return toHexString(md5.digest());
        } catch (Exception e) {
            ZljUtils.LOG().e("MD5", "读取文件md5失败", e);
            return null;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    ZljUtils.LOG().e("MD5", "读取文件IOException", e);
                }
            }
        }
    }

    /**
     * 对文件进行加密
     */
    @Override
    public String getMD5File(String filename) {
        InputStream fis = null;
        byte[] buffer = new byte[1024];
        int numRead = 0;
        MessageDigest md5;
        try {
            // 不存在则提前返回
            File file = new File(filename);
            if (!file.exists()) {
                return null;
            }
            fis = new FileInputStream(file);
            md5 = MessageDigest.getInstance("MD5");
            while ((numRead = fis.read(buffer)) > 0) {
                md5.update(buffer, 0, numRead);
            }
            return toHexString(md5.digest());
        } catch (Exception e) {
            ZljUtils.LOG().e("MD5", "读取文件md5失败", e);
            return null;
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    ZljUtils.LOG().e("MD5", "读取文件IOException", e);
                }
            }
        }
    }

    /**
     * toHexString
     *
     * @param b b
     * @return String
     */
    @Override
    public String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length * 2);
        for (int i = 0; i < b.length; i++) {
            sb.append(HEX_DIGITS[(b[i] & 0xf0) >>> 4]);
            sb.append(HEX_DIGITS[b[i] & 0x0f]);
        }
        return sb.toString();
    }

}
